package org.feng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.feng.utils.condition.Condition;
import org.feng.pojo.Result;
import org.feng.utils.UserUtil;
import org.feng.entity.pojo.User;
import org.feng.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.feng.utils.ExcelUtils;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import org.feng.entity.pojo.Log;
import org.feng.service.LogService;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author feng
 * @since 2021-08-27
 */
@RestController
@RequestMapping( "/system/log")
@AllArgsConstructor
@Api(value = "日志接口", tags = "日志")
public class LogController {

    private final LogService logService;
    private final UserService userService;

    /**
     * 导出
     */
    @ApiOperation(value = "导出", notes = "导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        ExcelUtils.writeExcel(response, logService.list(), Log.class);
    }

    /**
     * 列表
     */
    @ApiOperation(value = "列表", notes = "全部数据")
    @GetMapping
    public Result<List<Log>> list(Log log) {
        return Result.success(logService.list(Condition.getQueryWrapper(log)));
    }

    /**
     * 分页列表
     */
    @ApiOperation(value = "分页列表", notes = "分页数据")
    @GetMapping("/page")
    public Result<IPage<Log>> page(Log log) {
        User user = userService.getById(UserUtil.getUserId());
        return Result.success(logService.page(Condition.getPage(), Condition.getQueryWrapper(log)
                .eq("username", user.getUsername())
                .orderByDesc("create_time")));
    }

    /**
     * 详情
     */
    @ApiOperation(value = "数据详情", notes = "传输id")
    @GetMapping("/query")
    public Result<Log> detail(@RequestParam Long id) {
        return Result.success(logService.getById(id));
    }

    /**
     * 新增
     */
    @ApiOperation(value = "保存数据", notes = "传输对象")
    @PostMapping
    public Result<Boolean> save(@RequestBody Log log) {
        return Result.status(logService.save(log));
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新数据", notes = "传输对象")
    @PutMapping
    public Result<Boolean> update(@RequestBody Log log) {
        return Result.status(logService.updateById(log));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除数据", notes = "传输id数组")
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam String ids) {
        return Result.status(logService.removeByIds(Arrays.asList(ids.split(","))));
    }
}

